package com.haiheng.library_base.view.shape;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.haiheng.library_base.R;
import com.haiheng.library_base.utils.MetricsUtils;

import androidx.core.view.ViewCompat;


/**
 * Created by chow on 2019/8/10
 */

public class ShapeLinearLayout extends LinearLayout {

    private ShapeHelper shapeHelper;

    private int selectedTextColor;

    private int normalTextColor;

    private int strokeColor;

    private int solidColor;

    private int strokeWidth;

    private ShapeBuilder shapeBuilder;

    private float cornersRadius;
    private float cornersTopLeftRadius;
    private float cornersTopRightRadius;
    private float cornersBottomLeftRadius;
    private float cornersBottomRightRadius;
    //渐变
    private float shapeGradientAngle;
    private int shapeGradientCenterX;
    private int shapeGradientCenterY;
    private int shapeGradientGradientRadius;
    private int shapeGradientStartColor;
    private int shapeGradientCenterColor;
    private int shapeGradientEndColor;
    private boolean shapeGradientUseLevel;
    private boolean shapeUseSelector;
    private int shapeGradientType;

    //阴影
    private boolean showShadow;
    private int shadowColor;
    private float shadowColorAlpha;
    private int shadowLeftWidth;
    private int shadowTopWidth;
    private int shadowBottomWidth;
    private int shadowRightWidth;
    private int shadowCornersRadius;
    private int shadowCornersTopLeftRadius;
    private int shadowCornersTopRightRadius;
    private int shadowCornersBottomLeftRadius;
    private int shadowCornersBottomRightRadius;



    //散射半径
    private float mMaskRadius = 1.0f;
    //阴影渐变开始颜色
    private int mMaskStartColor;
    //阴影渐变结束颜色
    private int mMaskEndColor;
    private int mBgShowStartColor;
    private int mBgShowEndColor;
    private int mWidth;
    private int mHight;
    private Paint mMaskPaint;
    //背景的paint
    private Paint mBgPaint;
    private BlurMaskFilter mBlurMaskFilter;

    private ShadowDrawable mShadowDrawable;

    public ShapeLinearLayout(Context context) {
        this(context, null);
    }

    public ShapeLinearLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShapeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        shapeBuilder = new ShapeBuilder();
        init(context, attrs, defStyleAttr);

    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ShapeLinearLayout, defStyleAttr, 0);
        // 公共
        cornersRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shapeCornersRadius, 0); // 圆角
        cornersTopLeftRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shapeCornersTopLeftRadius, 0); // 左上圆角
        cornersTopRightRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shapeCornersTopRightRadius, 0); // 右上圆角
        cornersBottomRightRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shapeCornersBottomRightRadius, 0); // 右下圆角
        cornersBottomLeftRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shapeCornersBottomLeftRadius, 0); // 左下圆角
        solidColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeSolidColor, 0); // 填充色
        strokeWidth = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shapeStrokeWidth, 0); // 描边宽度
        strokeColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeStrokeColor, 0); // 描边色
        // 按压
        int pressSolidColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeSelectorPressedColor, 0);// 填充色
        // 选中
        int selectedSolidColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeSelectorNormalColor, 0);// 填充色
        int selectedStrokeColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeSelectorNormalColor, 0);// 描边色

        //字体颜色
//        normalTextColor = getCurrentTextColor();
        selectedTextColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeSelectorNormalColor, 0);
        selectedTextColor = selectedTextColor == 0 ? normalTextColor : selectedTextColor;


        //渐变
        shapeGradientAngle  =  typedArray.getFloat(R.styleable.ShapeLinearLayout_shapeGradientAngle,0f);
        shapeGradientCenterX  = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shapeGradientCenterX,0);
        shapeGradientCenterY  = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shapeGradientCenterY,0);
        shapeGradientGradientRadius  = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shapeGradientGradientRadius,0);
        shapeGradientStartColor  = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeGradientStartColor,0);
        shapeGradientCenterColor  = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeGradientCenterColor,0);
        shapeGradientEndColor  = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeGradientEndColor,0);
        shapeGradientUseLevel  = typedArray.getBoolean(R.styleable.ShapeLinearLayout_shapeGradientUseLevel,false);
        shapeUseSelector  = typedArray.getBoolean(R.styleable.ShapeLinearLayout_shapeUseSelector,false);
        shapeGradientType  = typedArray.getInt(R.styleable.ShapeLinearLayout_shapeGradientType,0);

        //阴影
        showShadow  =  typedArray.getBoolean(R.styleable.ShapeLinearLayout_showShadow,false);
        shadowColor  =  typedArray.getInt(R.styleable.ShapeLinearLayout_shadowColor,0);
        shadowColorAlpha  =  typedArray.getFloat(R.styleable.ShapeLinearLayout_shadowColorAlpha,0);
        shadowLeftWidth  =  typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowLeftWidth,0);
        shadowTopWidth  =  typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowTopWidth,0);
        shadowBottomWidth  =  typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowBottomWidth,0);
        shadowRightWidth  =  typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowRightWidth,0);
        shadowCornersRadius  =  typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowCornersRadius,0);
        shadowCornersTopLeftRadius  =  typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowCornersRadius,0);
        shadowCornersTopRightRadius  =  typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowCornersTopRightRadius,0);
        shadowCornersBottomLeftRadius  =  typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowCornersBottomLeftRadius,0);
        shadowCornersBottomRightRadius  =  typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowCornersBottomRightRadius,0);


        mMaskRadius = shadowLeftWidth;
        mMaskStartColor = shadowColor;
        mMaskEndColor = shadowColor;
        mBgShowStartColor = shapeGradientEndColor;
        mBgShowEndColor = shapeGradientStartColor;

        typedArray.recycle();

        shapeHelper = new ShapeHelper(this)
                .setRadius(new float[]{cornersTopLeftRadius, cornersTopLeftRadius,
                        cornersTopRightRadius, cornersTopRightRadius, cornersBottomRightRadius, cornersBottomRightRadius, cornersBottomLeftRadius, cornersBottomLeftRadius})
                .setRadius(cornersRadius).setStrokeWidth(strokeWidth)
                .setNormalDrawable(strokeColor, solidColor)
                .setPressDrawable(0, pressSolidColor)
                .setSelectedDrawable(selectedStrokeColor, selectedSolidColor);
        shapeHelper.setNormalDrawable();
//        mShadowDrawable = new ShadowDrawable( showShadow, shadowColor, shadowColorAlpha,
//                shadowTopWidth, shadowBottomWidth, shadowRightWidth, shadowCornersRadius,
//                shadowCornersTopLeftRadius, shadowCornersTopRightRadius ,
//                shadowCornersBottomLeftRadius, shadowCornersBottomRightRadius);
        initShape();


    }

    private void initShape() {
        shapeBuilder
                .setShapeType(ShapeBuilder.RECTANGLE)
                .setShapeCornersRadius(cornersRadius)
                .setShapeCornersTopLeftRadius(cornersTopLeftRadius)
                .setShapeCornersTopRightRadius(cornersTopRightRadius)
                .setShapeCornersBottomRightRadius(cornersBottomRightRadius)
                .setShapeCornersBottomLeftRadius(cornersBottomLeftRadius)
                .setShapeSolidColor(solidColor)
                .setShapeStrokeColor(strokeColor)
                .setShapeStrokeWidth(strokeWidth)
//                .setShapeStrokeDashWidth(strokeDashWidth)
//                .setShapeStrokeDashGap(strokeDashGap)
                .setShapeUseSelector(true)
//                .setShapeUseSelector(shapeUseSelector)
//                .setShapeSelectorNormalColor(selectorNormalColor)
//                .setShapeSelectorPressedColor(selectorPressedColor)
//                .setShapeGradientAngle((int) shapeGradientAngle)
//                .setShapeGradientGradientRadius(shapeGradientGradientRadius)
//                .setShapeGradientUseLevel(shapeGradientUseLevel)
//                .setShapeGradientCenterX(shapeGradientCenterX)
//                .setShapeGradientCenterY(shapeGradientCenterY)
//                .setShapeGradientStartColor(shapeGradientStartColor)
//                .setShapeGradientCenterColor(shapeGradientCenterColor)
//                .setShapeGradientEndColor(shapeGradientEndColor)
//                .setShapeGradientType(shapeGradientType)
                .into(this);

    }

    private void initPaint() {
        mMaskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    /***
     * 初始化背景paint
     */
    private void setBgPaint() {
        LinearGradient bgLinearGradient = new LinearGradient(0, 0, MetricsUtils.INSTANCE.dp2px(mWidth - 2 * mMaskRadius), 0
                , new int[]{mBgShowStartColor, mBgShowEndColor}
                , new float[]{0, .9F}
                , Shader.TileMode.CLAMP);
        mBgPaint.setShader(bgLinearGradient);
    }

    /***
     * 初始化阴影paint
     */
    private void setMaskPaint() {
        LinearGradient maskLinearGradient = new LinearGradient(0, 0, 6, 0
                , new int[]{mMaskStartColor, mMaskEndColor}
                , new float[]{0, .9F}
                , Shader.TileMode.CLAMP);
        mMaskPaint.setShader(maskLinearGradient);
        mBlurMaskFilter = new BlurMaskFilter(mMaskRadius, BlurMaskFilter.Blur.NORMAL);
        setLayerType(View.LAYER_TYPE_SOFTWARE, mMaskPaint);
        mMaskPaint.setMaskFilter(mBlurMaskFilter);
    }






    @Override
    public void setPressed(boolean pressed) {
        super.setPressed(pressed);
        if (pressed) {
            shapeHelper.setPressDrawable();
        }
    }



    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            shapeHelper.setSelectedDrawable();
//            setTextColor(selectedTextColor);
        } else {
            shapeHelper.setNormalDrawable();
//            setTextColor(normalTextColor);
        }
    }

    public void setSolidColor(int color) {
        this.solidColor = color;
        shapeHelper.setNormalDrawable(strokeColor, color);
    }

    public void setStrokeColor(int strokeColor) {
        this.strokeColor = strokeColor;
        shapeHelper.setStrokeColor(strokeColor);
        shapeHelper.setNormalDrawable(strokeColor, solidColor);
    }

    public void setStrokeWidth(int strokeWidth) {
        shapeHelper.setStrokeWidth(strokeWidth);
    }

    public void setRadius(int radius) {
        shapeHelper.setRadius(radius);
    }

}